﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OneMG.Phoenix.Service.Model;
using OneMG.Phoenix.Service.Contracts;
using OneMG.Phoenix.Service;

namespace OneMG.Phoenix.AdminSite.Views.Passport
{
    public partial class OrganizationList : System.Web.UI.Page
    {
        private static IOrganizationService IOrganization = new OrganizationService();


        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BindTree()
        {
            IList<OrganizationModel> orgList = IOrganization.GetByParentID(0);
            if (orgList.Count > 0)
            {
                OrganizationModel objorg = orgList[0];

                TreeNode child = new TreeNode();
                child.Value = objorg.OrgID.ToString();
                child.Text = objorg.OrgName;
                this.trvOrg.Nodes.Add(child);
                this.ConstructNode(objorg, child, 0);
            }
        }


        private string SelectOrgId
        {
            get
            {
                string str = "0";
                if ((this.trvOrg != null) && (this.trvOrg.SelectedNode != null))
                {
                    str = this.trvOrg.SelectedNode.Value;
                }
                return str;
            }
        }


        protected void trvOrg_SelectedNodeChanged(object sender, EventArgs e)
        {
            ViewState["ParentID"] = Int32.Parse(this.trvOrg.SelectedNode.Value);
            this.BindData();
            this.BindClientEvent();

        }


        #region 分页代码
        /// <summary>
        /// 绑定数据，不改变此方法
        /// </summary>
        /// <param name="intPage">页码</param>
        private void BindData()
        {
            IList<OrganizationModel> dtQuery = GetData();
            string order = gridHelper.GetOrderBy();

            DateTime dtDefault = new DateTime(1900, 1, 1);
            int CurrentPage = gridPager.CurrentPage;

            // 绑定
            dgResult.DataSource = dtQuery;
            dgResult.DataBind();
            dgResult.PageIndex = gridPager.CurrentPage;
            dgResult.PageSize = gridPager.PageSize;

            //gridPager.TotalItems = TotalRecord;
            gridPager.TotalItems = dtQuery.Count;
            gridPager.DataBind();

        }

        protected void gridPager_PageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        protected void btnGo_Click(object sender, System.EventArgs e)
        {
            BindData();
        }

        /// <summary>
        /// 查询数据，只改变这里的方法
        /// </summary>
        /// <returns>包含查询结果的数据集</returns>
        private IList<OrganizationModel> GetData()
        {
            int parentID = (int)ViewState["ParentID"];
            return IOrganization.GetByParentID(parentID);
        }
        #endregion

        /// <summary>
        /// 绑定客户端事件.
        /// </summary>
        private void BindClientEvent()
        {
            string str = "0";

            if (this.SelectOrgId == str)
            {
                this.btnAdd.Enabled = false;
            }
            else
            {
                this.btnAdd.Enabled = true;
                str = this.trvOrg.SelectedNode.Value;
                this.btnAdd.PostBackUrl = "OrganizationEdit.aspx?q=" + GetEncryptQueryString("Action=add&ParentID=" + str);
            }
        }


        protected void dgResult_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {

            int OrgID = (int)dgResult.DataKeys[e.RowIndex].Value;

            try
            {
                //更新删除标识deleteflag为true
                IOrganization.DeleteByOrgID(OrgID);
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "alert('操作成功!');location='/Passport/OrganizationList.aspx';", true);
            }
            catch (Exception ex)
            {
                lblMessage.Text = "操作失败,<br/>" + ex.Message;
                return;
            }
        }



        /// <summary>
        /// Constructs the node.
        /// </summary>
        /// <param name="dt">The dt.</param>
        /// <param name="curNodeData">The cur node data.</param>
        /// <param name="curNode">The cur node.</param>
        /// <param name="orgID">The org ID.</param>
        private void ConstructNode(OrganizationModel curNodeData, TreeNode curNode, int orgID)
        {
            int ParentID = 0;
            if (curNodeData != null)
            {
                ParentID = curNodeData.OrgID;
            }
            IList<OrganizationModel> orgList = IOrganization.GetByParentID(ParentID);
            foreach (OrganizationModel orgModel in orgList)
            {
                TreeNode child = new TreeNode();
                child.Text = orgModel.OrgName;
                child.Value = orgModel.OrgID.ToString();
                curNode.ChildNodes.Add(child);
                this.ConstructNode(orgModel, child, orgID);
            }
            if (curNode.Value == orgID.ToString())
            {
                curNode.Selected = true;
                curNode.Expand();
                for (TreeNode node2 = curNode.Parent; node2 != null; node2 = node2.Parent)
                {
                    node2.Expand();
                }
            }
        }

    }
}
